package studentdemo.ui;

import studentdemo.modul.Offer;
import studentdemo.modul.Course;
import studentdemo.modul.Class;
import studentdemo.server.OfferService;
import studentdemo.server.CourseService;
import studentdemo.server.ClassService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

public class OfferPanel extends JPanel {
    private OfferService offerService = new OfferService();
    private CourseService courseService = new CourseService();
    private ClassService classService = new ClassService();
    private JComboBox<Course> courseBox;
    private JComboBox<Class> classBox;
    private JButton addBtn, deleteBtn, refreshBtn;
    private JTable table;
    private DefaultTableModel tableModel;

    public OfferPanel() {
        setLayout(new BorderLayout());
        // 顶部操作区
        JPanel topPanel = new JPanel();
        courseBox = new JComboBox<>();
        classBox = new JComboBox<>();
        addBtn = new JButton("新增开设");
        deleteBtn = new JButton("删除开设");
        refreshBtn = new JButton("刷新");
        topPanel.add(new JLabel("课程:"));
        topPanel.add(courseBox);
        topPanel.add(new JLabel("班级:"));
        topPanel.add(classBox);
        topPanel.add(addBtn);
        topPanel.add(deleteBtn);
        topPanel.add(refreshBtn);
        add(topPanel, BorderLayout.NORTH);

        // 表格区
        String[] columns = {"课程编号", "课程名称", "班级编号", "班级名称"};
        tableModel = new DefaultTableModel(columns, 0) {
            public boolean isCellEditable(int row, int column) { return false; }
        };
        table = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

        // 事件绑定
        addBtn.addActionListener(e -> addOfferAction());
        deleteBtn.addActionListener(e -> deleteOfferAction());
        refreshBtn.addActionListener(e -> {
            loadCourseBox();
            loadClassBox();
            loadTable();
        });
        courseBox.addActionListener(e -> loadTableByCourse());
        classBox.addActionListener(e -> loadTableByClass());

        loadCourseBox();
        loadClassBox();
        loadTable();
    }

    private void loadCourseBox() {
        courseBox.removeAllItems();
        List<Course> courses = courseService.getCoursesByPage(1, 1000);
        for (Course c : courses) {
            courseBox.addItem(c);
        }
    }

    private void loadClassBox() {
        classBox.removeAllItems();
        List<Class> classes = classService.getAllClasses();
        for (Class cl : classes) {
            classBox.addItem(cl);
        }
    }

    private void loadTable() {
        tableModel.setRowCount(0);
        List<Offer> offers = offerService.getAllOffers();
        for (Offer offer : offers) {
            Course c = courseService.getCourseById(offer.getOcno());
            Class cl = classService.getClassById(offer.getOclno());
            if (c != null && cl != null) {
                tableModel.addRow(new Object[]{c.getCno(), c.getCname(), cl.getClno(), cl.getClname()});
            }
        }
    }

    private void loadTableByCourse() {
        Course c = (Course) courseBox.getSelectedItem();
        if (c == null) return;
        tableModel.setRowCount(0);
        List<Offer> offers = offerService.getOffersByCourse(c.getCno());
        for (Offer offer : offers) {
            Class cl = classService.getClassById(offer.getOclno());
            if (cl != null) {
                tableModel.addRow(new Object[]{c.getCno(), c.getCname(), cl.getClno(), cl.getClname()});
            }
        }
    }

    private void loadTableByClass() {
        Class cl = (Class) classBox.getSelectedItem();
        if (cl == null) return;
        tableModel.setRowCount(0);
        List<Offer> offers = offerService.getOffersByClass(cl.getClno());
        for (Offer offer : offers) {
            Course c = courseService.getCourseById(offer.getOcno());
            if (c != null) {
                tableModel.addRow(new Object[]{c.getCno(), c.getCname(), cl.getClno(), cl.getClname()});
            }
        }
    }

    private void addOfferAction() {
        Course c = (Course) courseBox.getSelectedItem();
        Class cl = (Class) classBox.getSelectedItem();
        if (c == null || cl == null) {
            JOptionPane.showMessageDialog(this, "请选择课程和班级");
            return;
        }
        Offer offer = new Offer(c.getCno(), cl.getClno());
        boolean success = offerService.addOffer(offer);
        if (success) {
            JOptionPane.showMessageDialog(this, "添加成功");
            loadTable();
        } else {
            JOptionPane.showMessageDialog(this, "添加失败，可能已存在该开设关系");
        }
    }

    private void deleteOfferAction() {
        int row = table.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(this, "请先选中一行");
            return;
        }
        String cno = (String) tableModel.getValueAt(row, 0);
        String clno = (String) tableModel.getValueAt(row, 2);
        int confirm = JOptionPane.showConfirmDialog(this, "确定要删除该开设关系吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (confirm == JOptionPane.YES_OPTION) {
            boolean success = offerService.deleteOffer(cno, clno);
            if (success) {
                JOptionPane.showMessageDialog(this, "删除成功");
                loadTable();
            } else {
                JOptionPane.showMessageDialog(this, "删除失败");
            }
        }
    }
} 